//
//  SEPrescriptionPickDrugsDetailCell.swift
//  SmartEyeForDoctor
//
//  Created by QDSG on 2021/5/12.
//

import UIKit

enum SEPrescriptionPickDrugsDetailCellType: Int {
    case drugName = 0
    case drugMeasure
    case drugFrequency
    case drugPosition
    case drugUsing
    case drugCount
    case confirm
    
    var pickType: CommonPickerViewType? {
        switch self {
        case .drugFrequency: return .drugFrequency
        case .drugPosition:  return .drugPosition
        case .drugUsing:     return .drugUsing
        default:             return nil
        }
    }
    
    var title: String? {
        switch self {
        case .drugName:      return "药品名称"
        case .drugMeasure:   return "剂量"
        case .drugFrequency: return "使用频次"
        case .drugPosition:  return "用药部位"
        case .drugUsing:     return "用法"
        case .drugCount:     return "数量"
        default:             return nil
        }
    }
    
    var arrowHidden: Bool {
        switch self {
        case .drugFrequency,
             .drugUsing,
             .drugPosition: return false
        default:            return true
        }
    }
    
    var textFieldEnabled: Bool {
        switch self {
        case .drugMeasure,
             .drugCount: return true
        default:         return false
        }
    }
    
    var keyboardType: UIKeyboardType {
        switch self {
        case .drugMeasure,
             .drugCount:    return .numberPad
        default:            return .default
        }
    }
    
    var descHidden: Bool {
        switch self {
        case .drugUsing,
             .drugFrequency,
             .drugPosition: return true
        default:            return false
        }
    }
}

class SEPrescriptionPickDrugsDetailCell: UITableViewCell, UITextFieldDelegate {
    
    var cellType: SEPrescriptionPickDrugsDetailCellType = .drugName
    
    @IBOutlet private weak var titleLabel: UILabel!
    
    @IBOutlet private weak var descLabel: UILabel! {
        didSet {
            descLabel.adjustsFontSizeToFitWidth = true
        }
    }
    
    @IBOutlet private weak var textField: UITextField! {
        didSet {
            textField.delegate = self
        }
    }
    
    @IBOutlet private weak var arrowView: UIImageView!
    
    private var viewModel = SEPrescriptionDrugPickViewModel()
    
    func update(with viewModel: SEPrescriptionDrugPickViewModel, cellType: SEPrescriptionPickDrugsDetailCellType) {
        self.viewModel = viewModel
        self.cellType = cellType
        titleLabel.text = cellType.title
        textField.keyboardType = cellType.keyboardType
        textField.isEnabled = cellType.textFieldEnabled
        arrowView.isHidden = cellType.arrowHidden
        descLabel.isHidden = cellType.descHidden
        switch cellType {
        case .drugName:
            textField.text = viewModel.drugModel.name
            descLabel.text = viewModel.drugModel.drugSpecification
        case .drugMeasure:
            textField.text = viewModel.drugModel.drugDose
            descLabel.text = "\(viewModel.drugModel.dosageUnit ?? "")/次"
        case .drugFrequency:
            textField.text = viewModel.drugModel.drugFrequency
        case .drugPosition:
            textField.text = viewModel.drugModel.drugPosition
        case .drugCount:
            textField.text = viewModel.drugModel.drugNumber
            descLabel.text = viewModel.drugModel.outPatientUnit
        case .drugUsing:
            textField.text = viewModel.drugModel.drugUsing
        default: break
        }
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        switch cellType {
        case .drugMeasure:
            viewModel.drugModel.drugDose = textField.text
        case .drugFrequency:
            viewModel.drugModel.drugFrequency = textField.text
        case .drugPosition:
            viewModel.drugModel.drugPosition = textField.text
        case .drugUsing:
            viewModel.drugModel.drugUsing = textField.text
        case .drugCount:
            viewModel.drugModel.drugNumber = textField.text
        default: break
        }
    }
}
